<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\facade\View;

class Node extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\User';

    public function index(): ?string
    {
        $keyword = $this->request->param('keyword');
        if ($keyword) {
            $map['code|nickname|mobile'] = $keyword;
        } else {
            $map['code'] = 'admin';
        }
        $first = $this->model::where($map)->field('id,code,team_id,level_id,nickname,mobile')->find();
        if ($first) {
            $level = $first->level ? $first->level->title : '暂无';
            $team_level = $first->team ? $first->team->title : '暂无';
            $first['title'] = '编号【' . $first->code . '】
                姓名【' . $first->nickname . '】
                手机号【' . $first->mobile . '】
                会员等级【' . $level . '】
                团队等级【' . $team_level . '】
               ';
        }

        View::assign('first', $first);
        return View::fetch();

    }

    public function getData()
    {
        try {

            $id = $this->request->param('id');
            $list = $this->model::where('node_id', $id)->field('id,code,team_id,level_id,nickname,mobile')->select();
            foreach ($list as &$v) {
                $level = $v->level ? $v->level->title : '暂无';
                $team_level = $v->team ? $v->team->title : '暂无';
                $v['title'] = '编号【' . $v['code'] . '】
                    姓名【' . $v->nickname . '】
                    手机号【' . $v->mobile . '】
                    会员等级【' . $level . '】
                    团队等级【' . $team_level . '】
                  ';
            }
            return json(['code' => 1, 'msg' => 'success', 'result' => $list]);

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }

    }


}
